define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'tongcheng/coupon/index' + location.search,
                    add_url: 'tongcheng/coupon/add',
                    edit_url: 'tongcheng/coupon/edit',
                    del_url: 'tongcheng/coupon/del',
                    multi_url: 'tongcheng/coupon/multi',
                    import_url: 'tongcheng/coupon/import',
                    table: 'tongcheng_coupon',
                }
            });

            var table = $("#table");

            // 在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
              let form = $("form", table.$commonsearch);

              $("input[name='zone_id']", form).addClass("selectpage").data("source", "tongcheng/zone/index").data("params", {"custom[status]": 1}).data("orderBy","id desc");

              Form.events.cxselect(form);
              Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'zone_id', title: __('Zone_id'), visible: false},
                        {
                            field: 'zone.name', title: __('Zone.name'), operate: false, formatter: function (value, row, index) {
                                return value + '(' + row.zone_id + ')';
                            }
                        },
                        {field: 'type', title: __('Type'), searchList: {"VOUCHER":__('Type VOUCHER'),"FULL":__('Type FULL'),"DISCOUNT":__('Type DISCOUNT')}, formatter: Table.api.formatter.normal},
                        {field: 'get_type', title: __('get_type'), searchList: {"index":__('get_type index'),"assign":__('get_type assign')}, formatter: Table.api.formatter.normal},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {
                          field: 'money', title: __('Money'), operate:'BETWEEN', formatter: function (value, row, index) {
                            if (row.type == 'DISCOUNT') {
                              return row.discount + '折';
                            } else {
                              return value;
                            }
                          }
                        },
                        {field: 'min_money', title: __('Min_money'), operate:'BETWEEN'},
                        {field: 'expire_day', title: __('Expire_day')},
                        {field: 'limit_use', title: __('limit_use')},
                        {
                          field: 'stocks', title: '剩余库存/发放总量', operate: false, formatter: function (value, row, index) {
                            return value + '/' + row.total;
                          }
                        },
                        {
                          field: 'used_count', title: '已使用数/已领取数', operate: false, formatter: function (value, row, index) {
                            return value + '/' + row.get_count;
                          }
                        },
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                          field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                          //formatter: Table.api.formatter.operate,
                          // 根据条件隐藏编辑、删除按钮
                          formatter: function (value, row, index) {
                            var that = $.extend({}, this);
                            var table = $(this.table).clone(true);

                            // 已被领取的，隐藏编辑、删除按钮
                            if (row.total != row.stocks) {
                              $(table).data('operate-edit', null);
                              $(table).data('operate-del', null);
                            }

                            that.table = table;
                            return Table.api.formatter.operate.call(that, value, row, index);
                          },
                          buttons: [
                            {
                              name: 'record',
                              text: '领取记录',
                              title: '领取记录',
                              classname: 'btn btn-xs btn-info btn-dialog',
                              icon: 'fa fa-list',
                              extend: 'data-area=\'["80%", "80%"]\'',
                              url: function (row) {
                                return 'tongcheng/coupon_record/index?coupon_id=' + row.id;
                              },
                            },
                            {
                              name: 'disable',
                              text: '禁用',
                              title: '禁用',
                              classname: 'btn btn-xs btn-primary btn-ajax btn-disable',
                              icon: 'fa fa-times',
                              url: 'tongcheng/coupon/disable',
                              refresh: true,
                              visible: function (row) {
                                return row.status == 1;
                              },
                              confirm: '确认禁用该优惠券吗？（禁用后该优惠券将不能被领取，已领取的仍然可以使用）',
                            }
                          ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'tongcheng/coupon/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'tongcheng/coupon/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'tongcheng/coupon/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

              // selectpage级联 - 子级
              $('#c-zone_id').on('change', function () {
                $("#c-user_id").selectPageClear();
              });
              $("#c-user_id").data("params", function () {
                const zone_id = $("input[name='row[zone_id]']").val();

                return {
                  custom: {
                    status: 'normal',
                    zone_id: zone_id
                  }
                };
              });
            }
        }
    };
    return Controller;
});
